%analyze the threshold function, manipulate its power to find optimal
%threshold power

number_of_nodes = 30;
params = ParamsSingleton.instance();

%simulation params
router_type = params.router_type_ROMER;
params.simulation_time_in_msec = 1000;
params.clock_tick_delta_in_msec = 100;

params.ROMER_threshold_power = 2;
params.should_use_constant_forward_probability = true;
params.constant_forward_probability = 1;

num_of_ticks_to_deposit_packets = 10;
packet_size_in_bytes = 500;
number_of_packets_to_deposit_in_each_tick = 100;
ticks_to_deposit_packets = 1:num_of_ticks_to_deposit_packets;

packet_size_in_bytes = 500;
number_of_packets_to_deposit_in_each_tick = 100;
%%%%%%%%%%%%%%%%%%%%%%%%

source_node_id = 26;
dest_node_id = 13;

SingletonInitializer.create_all_singletons();
statistics = StatisticsManager.instance();

power_vector = 0:0.2:2.5;

%simulation statistics
delivery_rates_vector = zeros(1, length(power_vector));
number_of_different_paths_vector = zeros(1, length(power_vector));
network_load = zeros(1, length(power_vector));
num_of_drops = zeros(1, length(power_vector));

topology_file_name = ['topology_', num2str(number_of_nodes), '_nodes.mat'];

for i = 1:length(power_vector)
     params.ROMER_threshold_power = power_vector(i);
     
    SingletonInitializer.reinit_all_singletons();
    topology = Topology.read_topology_from_file(topology_file_name, router_type);
    
    statistics = Simulator.run_single_depositor_in_unloaded_network(topology, source_node_id, dest_node_id, packet_size_in_bytes, number_of_packets_to_deposit_in_each_tick, ticks_to_deposit_packets);
    
    number_of_different_paths_vector(i) = statistics.number_of_disjoint_paths;
    delivery_rates_vector(i) = statistics.delivery_rate;
    network_load(i) = statistics.avg_network_load;
    num_of_drops(i) = statistics.romer_num_of_packets_dropped;
end

% draw the results

figure();
grid minor;
hold on;
xlabel('ROMER threshold power');
ylabel('number of disjoint paths');
title('number of disjoint routing paths = f(threshold power)');
plot(power_vector, number_of_different_paths_vector, 'Color', 'blue');
hold off;

figure();
grid minor;
hold on;
xlabel('ROMER threshold power');
ylabel('delivery ratio (%)');
title('delivery ratio = f(threshold power)');
plot(power_vector, delivery_rates_vector, 'Color', 'blue');
hold off;

figure();
grid minor;
hold on;
xlabel('ROMER threshold power');
ylabel('avg network load (KB)');
title('avg network load = f(threshold power)');
plot(power_vector, network_load, 'Color', 'blue');
hold off;

figure();
grid minor;
hold on;
xlabel('ROMER threshold power');
ylabel('number of packets beneath threshold');
title('number of packets beneath threshold = f(threshold power)');
plot(power_vector, num_of_drops, 'Color', 'blue');
hold off;
